import time

from spider.util import CrawlUtil

class Crawl:
    crawl_util=CrawlUtil()

    def crawl_music(self,song_name,log_path,music_path):
        wait_time=5
        print("开始搜索音乐：{0}".format(song_name))

        file_open = open(log_path, "w+", encoding='utf-8')
        self.crawl_util.create_dir(music_path)
        music_list = self.crawl_util.get_music(song_name)
        for music in music_list:
            time.sleep(wait_time)
            print('找到音乐(编号：{0}，名称：{1}，作者：{2}，专辑：{3})，休息{4}秒'.format(
                music.id, music.name, music.artist, music.album,wait_time))
            location = self.crawl_util.get_music_location(music.id)
            time.sleep(wait_time)
            music.path = self.crawl_util.get_music_path(location)
            file_open.write("编号：{0},名称：{1},作者：{2},专辑：{3},下载路径：{4}\n"
                            .format(music.id, music.name, music.artist, music.album, music.path))
            #file_open.flush()
            print('写入音乐信息，开始下载音乐'.format(
                music.id,music.name,music.artist,music.album))

            self.crawl_util.download_music(music, music_path)
            print('音乐下载完成，休息{0}秒'.format(wait_time))

        file_open.close()
        print('音乐下载完成')







